package com.chx.three.mama.activities;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.chx.three.mama.R;
import com.chx.three.mama.model.Address;
import com.chx.three.mama.utils.Constants;
import com.chx.three.mama.view.Topbar;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import org.json.JSONObject;

import javax.xml.transform.ErrorListener;

@ContentView(R.layout.activity_mail_address)
public class MailAddressActivity extends ActionBarActivity implements View.OnClickListener, Topbar.onTopbarClickListener {
    public static final int CHECK_CITY = 101;
    @ViewInject(R.id.address_name)
    EditText mName;
    @ViewInject(R.id.address_phone)
    EditText mPhone;
    @ViewInject(R.id.address_check_address)
    LinearLayout mCheckAddress;
    @ViewInject(R.id.address_address)
    TextView mAddress;
    @ViewInject(R.id.address_detail_address)
    EditText mDetailAddress;
    @ViewInject(R.id.address_mail_num)
    EditText mMailNum;
    @ViewInject(R.id.address_save)
    Button mSave;
    @ViewInject(R.id.topbar)
    Topbar topbar;
    private Address address;

    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        setDialog();
        initData();
        mCheckAddress.setOnClickListener(this);
        topbar.setOnTopbarClickListener(this);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        RequestQueue requestQueue= Volley.newRequestQueue(this);
        JsonObjectRequest request =
                new JsonObjectRequest(
                        "",
                        new JSONObject(),
                        new Response.Listener<JSONObject>() {


                            @Override
                            public void onResponse(JSONObject object) {
                                JSONObject data = object.optJSONObject("data");
                                address = JSON.parseObject(data.toString(), Address.class);
                                if(address!=null){
                                    if(TextUtils.isEmpty(address.getRecvname())){
                                        mName.setText(address.getRecvname());
                                    }

                                    if(TextUtils.isEmpty(address.getMobile())){
                                        mPhone.setText(address.getMobile());
                                    }
                                    StringBuilder sb=new StringBuilder();
                                    if(TextUtils.isEmpty(address.getP_name())){
                                        sb.append(address.getP_name());
                                        sb.append(" ");
                                    }
                                    if(TextUtils.isEmpty(address.getC_name())){
                                        sb.append(address.getC_name());
                                        sb.append(" ");
                                    }
                                    if(TextUtils.isEmpty(address.getA_name())){
                                        sb.append(address.getA_name());
                                        sb.append(" ");
                                    }
                                    if(TextUtils.isEmpty(sb.toString())){
                                        mAddress.setText(sb.toString());
                                    }

                                    if(TextUtils.isEmpty(address.getStreet())){
                                        mDetailAddress.setText(address.getStreet());
                                    }
                                    if(TextUtils.isEmpty(address.getPostcode())){
                                        mMailNum.setText(address.getPostcode());
                                    }

                                }
                                dialog.cancel();
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Toast.makeText(MailAddressActivity.this, "网络不给力", Toast.LENGTH_SHORT).show();
                            }
                        }
                );
        requestQueue.add(request);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0,R.anim.push_right_out);
    }

    /**
     * 开头显示加载框
     */
    private void setDialog() {
        dialog = new Dialog(this);
        dialog.setContentView(R.layout.layout_dialog_loading);
        dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.color.transparent));
        dialog.show();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.address_check_address:
                Intent intent = new Intent(this, CityCheckActivity.class);
                intent.putExtra(Constants.BundleKey.KEY_SETTING_ADDRESS,address);
                startActivityForResult(intent, CHECK_CITY);
                break;
        }
    }

    @Override
    public void onLeftButtonClick() {
        finish();
    }

    @Override
    public void onRightButtonClick() {

    }

    @Override
    public void onSearchButtonClick() {

    }
}
